package co.za.itweb.itwebpro;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class sAdapter extends BaseAdapter {
	private Activity activity2;
	String[] itemID;
	String[] titles;
	String[] images;
	String[] summary;
	int currentapiVersion = Build.VERSION.SDK_INT;
	private LayoutInflater inflater2;

	
	@Override
	public int getCount() {
		
		return itemID.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public sAdapter(Activity act, String[] item, String[] tit,
			String[] img, String[] sum) {
		titles = tit;
		images = img;
		summary = sum;
		itemID = item;
		activity2 = act;
		inflater2 = (LayoutInflater) activity2
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private static class ViewHolder2 {
		public ImageView iconView;
		public TextView titleTextView;
		public TextView bottomText;
		public ImageLoader imageLoader;
		public LazyLoadImage icondownloader;
		

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder2 holder2;
		
		//if (convertView == null) {
			convertView = (ViewGroup) inflater2.inflate(R.layout.imageslist,
					null);
			holder2 = new ViewHolder2();

			holder2.titleTextView = (TextView) convertView
					.findViewById(R.id.TextOnImage);
			holder2.bottomText = (TextView) convertView
					.findViewById(R.id.TextTitle);
			holder2.iconView = (ImageView) convertView
					.findViewById(R.id.oneimage);
			holder2.imageLoader = new ImageLoader(activity2);
			holder2.icondownloader = new LazyLoadImage(holder2.iconView);
			convertView.setTag(holder2);

		//} else {

			holder2 = (ViewHolder2) convertView.getTag();

		//}
			
			
		if (images[position].length() > 4) {

			Bitmap avatar = holder2.icondownloader
					.getBitmapFromCache(MainActivity.cachedir + "/"
							+ images[position]);

			if (avatar == null) {
				holder2.icondownloader.execute(MainActivity.cachedir + "/"
						+ images[position]);

			} else {

				holder2.iconView.setImageBitmap(avatar);
			}

			holder2.titleTextView.setText(titles[position]);
			holder2.bottomText.setVisibility(View.GONE);
		} else {
			holder2.iconView.setBackgroundColor(Color.parseColor("#ff050505"));
			if (summary.length <= position && summary[position].length() > 90) {
				String it = summary[position].substring(0, 90);
				it = it + "...";
				holder2.titleTextView.setText(it);
			} else {
				holder2.titleTextView.setText(summary[position]);
			}

			holder2.titleTextView.setHeight(250);

			holder2.bottomText.setText(titles[position]);
			holder2.bottomText.setTypeface(null, Typeface.BOLD);
		}
			

		return convertView;

	}

}
